#ifndef HTTPCLIENT_H
#define HTTPCLIENT_H

//#include <string>
//#include <sstream>
//#include <arpa/inet.h>
//#include <cstring>
//#include <fcntl.h>
//#include <iostream>
//#include <netinet/in.h>
//#include <sys/socket.h>
//#include <unistd.h>
//#include <QDebug>


#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <cstring>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>

/**
 * @brief 基于TCP 封装HTTP协议
 */
class HttpClient {
public:
    HttpClient();
    ~HttpClient();

    /**
     * @brief setUrl
     * @param ip
     * @param port
     */
    void setUrl(const std::string& host, int port);

    bool get(const std::string& path);
    bool post(const std::string& path, const std::string& data) ;
    bool put(const std::string& path, const std::string& data);
    bool del(const std::string& path) ;
    bool head(const std::string& path);

    void addHeader(const std::string& header) ;
    std::vector<std::string> getHeaders() const ;
    std::string getBody() const ;

private:
    /**
     * @brief 链接到服务器
     * @return
     */
    bool connectToServer();

    /**
     * @brief 发送http请求
     * @return
     */
    bool sendHttpRequest(const std::string& method,
                         const std::string& path, const std::string& data = "");
    /**
     * @brief 读取响应
     * @param response
     * @return
     */
    bool readHttpResponse(std::string& response) ;

    /**
     * @brief 解析响应
     * @param response
     */
    void parseResponse(const std::string& response);
private:
    std::string m_host;
    int m_port;
    int m_sock;
    std::vector<std::string> m_headers;
    std::string m_body;


};

#endif // HTTPCLIENT_H
